<?php

// +----------------------------------------------------------------------
// |ZBPHP[基于ThinkPHP5.1开发]
// +----------------------------------------------------------------------
// | Copyright (c) 2016-2019 http://www.benbenwangluo.com
// +----------------------------------------------------------------------
// | Author 似水星辰 [ 2630481389@qq.com ]
// +----------------------------------------------------------------------
// | 中犇软件 技术六部 出品
// +----------------------------------------------------------------------
namespace app\order\event;

use think\facade\Log;
/**
 *  自动确认收货钩子
 *  @author hu
 */
class ReceiveOrder {
    
    /**
     * 批量自动确认收货
     * @author 晓风
     * @return int
     */
    public function run($playload = [])
    {        
        $timeout = module_config('order.order_receive') * 86400;
        $order_map[] = ['order.status', '=', 2];
        $order_map[] = ['order.order_type', 'in', [3]];
        $order_map[] = ['order_goods_express.shipping_time', '<', (time() - $timeout)];
        //排除售后订单
        $order_map[] = ['order.order_sn', 'not in', function($query){
            $query->name("order_refund")->where("status","in",[0,1,2])->field("order_sn");
        }];
        $orders = \app\common\model\Order::view("order",true)
                ->view("order_goods_express",false,"order.order_sn = order_goods_express.order_sn")         
                ->where($order_map)
                ->select();        
       
        foreach ($orders as $order) {   
            $this->receiveOrder($order);
        }
        return 1;
    }
    //确认收货
    protected function receiveOrder($order){        
     
        try {         
            \app\order\model\OrderGoods::receiveOrder($order);        
        } catch (\Exception $e) {   
            Log::error($e->getMessage());
        }
    }
}
